﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using FluentETL;
using FluentETL.Impl;

namespace FluentETL_Examples
{
    public static class Example_Join
    {
        public static void Run()
        {
            string dataDir = ConfigurationManager.AppSettings["DataDirectory"];
            string inputFile_lastNames = Path.Combine(dataDir, "lastnames.txt");
            string inputFile_firstNames = Path.Combine(dataDir, "firstnames_male.txt");

            ISource src_firstNames = new TextSource()
                                        .From(inputFile_firstNames)
                                        .ParseAsDelimited(',', false);

            ISource src_lastNames = new TextSource()
                                        .From(inputFile_lastNames)
                                        .ParseAsDelimited(',', false);

            // This source makes all possible combinations of first and last names
            ISource src_combined = new ObjectSource()
                                    .From(from rowLastName in src_lastNames.Rows
                                            from rowFirstName in src_firstNames.Rows
                                            select new { LastName = rowLastName["Field0"],
                                                         FirstName = rowFirstName["Field0"] });

            IDestination dest = new TextDestination()
                                    .To(Console.Out)
                                    .WriteAs(items =>
                                    {
                                        string line = "";
                                        foreach (var item in items)
                                            line += (item.Name + " = " + item.Value) + "\t";
                                        return line;
                                    });

            Transform t = new Transform(src_combined, dest)
                                    .AutoMap()
                                    .Execute();
        }
    }
}
